Imports System.Drawing
Public Module mdManejoArchivos
    Public Function resizeImagen(ByVal img As System.Drawing.Bitmap, ByVal tamanoMaximo As Drawing.Size, Optional ByVal calidad As Integer = 75) As Byte()
        img.RotateFlip(Drawing.RotateFlipType.Rotate180FlipNone)
        img.RotateFlip(Drawing.RotateFlipType.Rotate180FlipNone)

        Dim tamanoThumb As Drawing.Size = bitworksComunesV3.mdManejoArchivos.tamanoThumbnail(img.Size, tamanoMaximo)
        Dim imagenResultado As Drawing.Bitmap = img.GetThumbnailImage(tamanoThumb.Width, tamanoThumb.Height, AddressOf thumbCallback, System.IntPtr.Zero)

        Dim sImagen As System.IO.MemoryStream = bitworksComunesV3.mdManejoArchivos.JPGaStream(imagenResultado, calidad)
        Dim aux As Byte() = sImagen.GetBuffer
        sImagen.Close()
        imagenResultado.Dispose()
        imagenResultado = Nothing
        Return aux
    End Function

    Public Function tamanoThumbnail(ByVal tamanoOrg As Drawing.Size, ByVal maximoTamano As Drawing.Size) As Drawing.Size
        Dim aux As New Drawing.Size(tamanoOrg.Width, tamanoOrg.Height)
        'primera ajustar ancho
        Dim relacionAncho As Single
        Dim relacionAlto As Single
        If aux.Width > maximoTamano.Width Then
            relacionAncho = maximoTamano.Width / tamanoOrg.Width
            aux.Width = maximoTamano.Width
            aux.Height = aux.Height * relacionAncho
        End If
        If aux.Height > maximoTamano.Height Then
            relacionAlto = maximoTamano.Height / aux.Height
            aux.Height = maximoTamano.Height
            aux.Width = aux.Width * relacionAlto
        End If
        Return aux
    End Function

    Public Function JPGaStream(ByVal img As Drawing.Bitmap, ByVal calidad As Integer) As System.IO.MemoryStream
        Dim codecs As System.Drawing.Imaging.ImageCodecInfo() = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
        Dim c As System.Drawing.Imaging.ImageCodecInfo
        Dim codecJPG As System.Drawing.Imaging.ImageCodecInfo
        For Each c In codecs
            If c.MimeType = "image/jpeg" Then codecJPG = c
        Next

        Dim parametrosCodec As New Drawing.Imaging.EncoderParameters()
        parametrosCodec.Param(0) = New Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, calidad)
        Dim aux As New System.IO.MemoryStream()
        img.Save(aux, codecJPG, parametrosCodec)
        img.Dispose()
        Return aux
    End Function

    Private Function thumbCallback() As Boolean

    End Function

    Public Function getArrayCampoImagen(ByVal cnn As Data.IDbConnection, ByVal cmd As Data.IDbCommand, ByVal tamano As Drawing.Size) As Byte()
        Dim ms As System.IO.MemoryStream = getStreamCampo(cnn, cmd)
        Dim img As Drawing.Bitmap
        img = New Drawing.Bitmap(ms)
        ms.Close()
        Return bitworksComunesV3.resizeImagen(img, tamano)
    End Function

    Public Function getStreamCampo(ByVal cnn As Data.IDbConnection, ByVal cmd As Data.IDbCommand) As System.IO.MemoryStream
        Dim aux As Boolean = False
        Dim cerrar As Boolean = False
        If cnn.State = ConnectionState.Closed Then
            cerrar = True
            cnn.Open()
        End If

        Dim dr As Data.IDataReader = cmd.ExecuteReader
        Dim arrByte As Byte()
        If dr.Read Then
            If dr(0) Is System.DBNull.Value = False Then
                arrByte = dr(0)
                aux = True
            End If
        End If
        dr.Close()
        Dim ms As System.IO.MemoryStream
        If aux Then
            ms = New System.IO.MemoryStream(arrByte)
        End If
        If cerrar Then cnn.Close()
        Return ms
    End Function

    Public Function streamAArray(ByVal archivo As System.IO.Stream) As Byte()
        Dim arr As Byte()
        ReDim arr(archivo.Length - 1)
        archivo.Read(arr, 0, archivo.Length)
        Return arr
    End Function

    Public Function convertFileToArrByte(ByVal rutaFile As String) As Byte()
        Dim docStream As System.IO.Stream
        docStream = System.IO.File.Open(rutaFile, IO.FileMode.Open, IO.FileAccess.Read)
        Dim fileData(CInt(docStream.Length)) As Byte
        docStream.Read(fileData, 0, fileData.Length)
        docStream.Close()
        Return fileData
    End Function

    Public Function convertStringToArray(ByVal s As String) As Byte()
        Return New System.Text.UTF8Encoding().GetBytes(s)
    End Function

    Public Sub convertArrByteToFile(ByVal arrByte() As Byte, ByVal rutaDestino As String)
        Dim directorio As String = rutaDestino.Substring(0, InStrRev(rutaDestino, "\"))
        If System.IO.Directory.Exists(directorio) = False Then System.IO.Directory.CreateDirectory(directorio)
        Dim s As System.IO.Stream = System.IO.File.Open(rutaDestino, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
        s.Write(arrByte, 0, arrByte.GetLength(0))
        s.Close()
    End Sub
    Public Function textoEnImagen(ByVal texto As String, ByVal tipoLetra As Drawing.Font, ByVal foreColor As Drawing.Brush, ByVal posicion As Drawing.Point, ByVal img As Drawing.Bitmap) As Drawing.Bitmap
        Dim aux As New Bitmap(img.Width, img.Height)
        Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(aux)
        g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height))
        g.DrawString(texto, tipoLetra, foreColor, posicion.X, posicion.Y)
        g.Dispose()
        Return aux
    End Function

    Public Function unirImagenes(ByVal img1 As Drawing.Bitmap, ByVal img2 As Drawing.Bitmap, ByVal posicionSegundaImagen As enPosicionesImagenes, ByVal tipoUnion As enUnionImagenes, Optional ByVal alineacion As enImagenesAlineacion = enImagenesAlineacion.izquierdaArriba) As Bitmap

        Dim tamanImagenFinal As Drawing.Size = tamanoResultado(img1, img2, posicionSegundaImagen, tipoUnion)
        Dim posImagen1 As Point = calcularPosPrimeraImage(img1, img2, posicionSegundaImagen, tipoUnion)
        Dim posImagen2 As Point = calcularPosSegundaImage(img1, img2, posicionSegundaImagen, tipoUnion)


        Dim aux As New Bitmap(tamanImagenFinal.Width, tamanImagenFinal.Height)

        Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(aux)
        g.DrawImage(img1, New Rectangle(posImagen1.X, posImagen1.Y, img1.Width, img1.Height))
        g.DrawImage(img2, New Rectangle(posImagen2.X, posImagen2.Y, img2.Width, img2.Height))
        g.Dispose()
        Return aux

    End Function

    Private Function tamanoResultado(ByVal img1 As Drawing.Bitmap, ByVal img2 As Drawing.Bitmap, ByVal posicionSegundaImagen As enPosicionesImagenes, ByVal tipoUnion As enUnionImagenes) As Drawing.Size
        Dim aux As New Drawing.Size(0, 0)

        If posicionSegundaImagen = enPosicionesImagenes.derecha OrElse posicionSegundaImagen = enPosicionesImagenes.izquierda Then
            If img1.Height > img2.Height Then
                aux.Height = img1.Height
            Else
                aux.Height = img2.Height
            End If

            If img1.Width > img2.Width Then
                aux.Width = img1.Width
                If tipoUnion = enUnionImagenes.externa Then
                    aux.Width += img2.Width
                End If
            Else
                aux.Width = img2.Width
                If tipoUnion = enUnionImagenes.externa Then
                    aux.Width += img1.Width
                End If
            End If
        ElseIf posicionSegundaImagen = enPosicionesImagenes.arriba OrElse posicionSegundaImagen = enPosicionesImagenes.abajo Then
            If img1.Height > img2.Height Then
                aux.Height = img1.Height
                If tipoUnion = enUnionImagenes.externa Then
                    aux.Height += img2.Height
                End If
            Else
                aux.Height = img2.Height
                If tipoUnion = enUnionImagenes.externa Then
                    aux.Height += img1.Height
                End If
            End If
            If img1.Width > img2.Width Then
                aux.Width = img1.Width
            Else
                aux.Width = img2.Width
            End If
        End If
        Return aux
    End Function
    Private Function calcularPosSegundaImage(ByVal img1 As Drawing.Bitmap, ByVal img2 As Drawing.Bitmap, ByVal posicionSegundaImagen As enPosicionesImagenes, ByVal tipoUnion As enUnionImagenes) As Drawing.Point
        Dim aux As New Drawing.Point(0, 0)
        If tipoUnion = enUnionImagenes.centradasHorVer Then
            If img1.Width > img2.Width Then
                aux.X = img1.Width / 2 - img2.Width / 2
            Else
                aux.X = img2.Width / 2 - img1.Width / 2
            End If
            If img1.Height > img2.Height Then
                aux.Y = img1.Height / 2 - img2.Height / 2
            Else
                aux.Y = img2.Height / 2 - img1.Height / 2
            End If
            Return aux
        End If

        Select Case posicionSegundaImagen
            Case enPosicionesImagenes.abajo
                aux.Y = img1.Height
                If tipoUnion = enUnionImagenes.interna Then
                    aux.Y -= img2.Height
                End If
            Case enPosicionesImagenes.derecha
                aux.X = img1.Width
                If tipoUnion = enUnionImagenes.interna Then
                    aux.X -= img2.Width
                End If
        End Select
        If aux.Y < 0 Then aux.Y = 0
        If aux.X < 0 Then aux.X = 0
        Return aux
    End Function
    Private Function calcularPosPrimeraImage(ByVal img1 As Drawing.Bitmap, ByVal img2 As Drawing.Bitmap, ByVal posicionSegundaImagen As enPosicionesImagenes, ByVal tipoUnion As enUnionImagenes) As Drawing.Point
        Dim aux As New Drawing.Point(0, 0)
        If tipoUnion = enUnionImagenes.centradasHorVer Then
            Return aux
        End If

        Select Case posicionSegundaImagen
            Case enPosicionesImagenes.arriba
                aux.Y = img2.Height
                If tipoUnion = enUnionImagenes.interna Then
                    aux.Y = 0
                End If
            Case enPosicionesImagenes.izquierda
                aux.X = img2.Width
                If tipoUnion = enUnionImagenes.interna Then
                    aux.X = 0
                End If
        End Select
        If aux.Y < 0 Then aux.Y = 0
        If aux.X < 0 Then aux.X = 0
        Return aux
    End Function

    Public Enum enPosicionesImagenes
        izquierda = 1
        arriba = 2
        derecha = 3
        abajo = 4
    End Enum
    Public Enum enUnionImagenes
        interna = 1
        externa = 2
        centradasHorVer = 100
    End Enum
    Public Enum enImagenesAlineacion
        izquierdaArriba = 1
        centro = 2
        abajoderecha = 3
    End Enum

End Module
